<?php 
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;

class Admin extends Controller
{
	
	  //登录
	  public function login()
	  {
	        $data=Request::instance()->post();
	        if(empty($data)|| !isset($data['username']) || $data['username']=='' || !isset($data['password']) || $data['password']=='')
	        {
	               $arr['code']=1;
	               $arr['msg']='用户名密码错误';
	               $arr['data']=[];
	               return json($arr);
	        }
	        $res=db::table('admin')->where($data)->find();

	        $preg_username= '/^[\x{4e00}-\x{9fa5}]{2,5}$u';


	        if(!preg_match($preg_username, $data['username']))
	        {
	        	   $arr['code']=1;
	               $arr['msg']='用户名要求必须是2到5位的汉字';
	               $arr['data']=[];
	               return json($arr);
	        }
	          $preg_password = '/^\S{6,18}$/';

	        if(!preg_match($preg_password, $data['password']))
	        {
	        	   $arr['code']=1;
	               $arr['msg']='密码要求必须6到18位非空字符串';
	               $arr['data']=[];
	               return json($arr);
	        }


	        if(!$res)
	        {
	              $arr['code']=1;
	               $arr['msg']='用户名密码错误';
	               $arr['data']=[];
	               return json($arr);
	        }
	            $arr['code']=0;
	            $arr['msg']='登录成功';
	            $arr['data']=[];
	            return json($arr);
	  }

      //展示
	 public function show()
     {
          if(!$_GET){
          	    return json(['msg'=>'无此信息','code'=>1,'data'=>'','status'=>1]);
          }
          $page=$_GET['page'];
          $limit=$_GET['limit'];
          $data=Db::table('good')->paginate($limit,false,['page'=>$page]);

          if(!$data){
          	     return json(['msg'=>'查询是失败','code'=>1,'data'=>'','status'=>1]);
          } 
                 return json(['msg'=>'查询成功','code'=>0,'data'=>$data,'status'=>0]);
     }
      //删除
    public function del()
    {
         $id=Request::instance()->GET('id','');

         $where['goods_id']=$id;
         $res=Db::table('good')->delete($id);

          if(!$res){
          	     return json(['msg'=>'删除失败','code'=>1,'data'=>'','status'=>1]);
          } 
                 return json(['msg'=>'删除成功','code'=>0,'data'=>$res,'status'=>0]);
    }
    //2删除
     public function dels()
    {
         $id=Request::instance()->delete('id','');

         $res=Db::table('good')->where('goods_id',$id)->delete();

          if(!$res){
                 return json(['msg'=>'删除失败','code'=>1,'data'=>'','status'=>1]);
          } 
                 return json(['msg'=>'删除成功','code'=>0,'data'=>$res,'status'=>0]);
    }
     //2删除
    public function delOne()
    {
        $id=request()->delete('id',0);

        if($id==0 || !is_numeric($id))
        {
              $arr['code']=1;
              $arr['msg']='提交数据参数有误';
              $arr['data']=[];

              return json($arr);
        }
        $res=db::table('good')->where('id',$id)->delete();

        if(!$res){
             $arr['code']=1;
             $arr['msg']='删除失败';
             $arr['data']=[];
             return json($arr);
        }
             $arr['code']=0;
             $arr['msg']='删除成功';
             $arr['data']=$res;
             return json($arr);


    }
     //修改
    public function updat()
    {     $arr=input('get.');

          $data=model('Good')->update($arr);
           if(!$data){
          	     return json(['msg'=>'修改失败','code'=>1,'data'=>'','status'=>1]);
          } 
                 return json(['msg'=>'修改成功','code'=>0,'data'=>$data,'status'=>0]);
    }

      //搜索
   public function search()
   {
       $search=input('get.');
       $page=input('get.page');
       $limit=input('get.limit');

       $res=model('Good')->getsearch($page,$limit,$search);
         if(!$res){
                 return json(['msg'=>'搜索失败','code'=>1,'data'=>'','status'=>1]);
          } 
                 return json(['msg'=>'搜索成功','code'=>0,'data'=>$res,'status'=>0]);

   }
   //多搜索
   public function sou()
   {
   	     $limit=request()->get('limit',3);
   	     $search=Request()->get('search','');
   	     $where=[];

          $goods_name=request()->get('goods_name','');
          if($goods_name !='')
          {
                $where['good.goods_name']=array('like',"%$goods_name%");
          }
          $type_id=request()->get('type_id','');
          if($type_id !='')
          {
                $where['good.type_id']=array('like',"%$type_id%");
          }
          $goods_show=request()->get('goods_show','');
          if($goods_show !='')
          {
                $where['good.goods_show']=array('like',"%$goods_show%");
          }
        $res = Db::table('good')->where( $where )->paginate( $limit, false, [ 'page'=> $page ] );
        // field( 'goods.*, t.type_name')->join('type t', 'type.type_id = goods.type_id' )

        $arr['code'] = 0;
        $arr['msg'] = 'ok';
        $arr['data'] = $res;
        return json($arr);
   }
	   public function upload(){
	    // 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('file');
	    // 移动到框架应用根目录/public/uploads/ 目录下
	    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
	    if($info){
	        // 成功上传后 获取上传信息
	        // 输出 jpg
	           $data['src']=$file->getError();

	           $data['code']=0;
	           $data['msg']='ok';
	           $data['data']=$data;
	           return json($data);
	        // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
	        // echo $info->getSaveName();
	        // 输出 42a79759f284b767dfcb2a0197904287.jpg
	        // echo $info->getFilename(); 
	    }else{
	        // 上传失败获取错误信息
	      

	           $data['code']=1;
	           $data['msg']=$file->getError();
	           $data['data']=[];
	           return json($data);
	    }
	}
  public function add()
  {
       $arr=input('post.');

       $res=db::table('Good')->insert($arr);

        if(!$res){
             $arr['code']=1;
             $arr['msg']='添加失败';
             $arr['data']=[];
             return json($arr);
        }
             $arr['code']=0;
             $arr['msg']='添加成功';
             $arr['data']=$res;
             return json($arr);
  }
   //文件上传
   public function adds()
  {
       $arr=input('post.');
        unset($arr['file']);
       $res=db::table('Good')->insert($arr);

        if(!$res){
             $arr['code']=1;
             $arr['msg']='添加失败';
             $arr['data']=[];
             return json($arr);
        }
             $arr['code']=0;
             $arr['msg']='添加成功';
             $arr['data']=$res;
             return json($arr);
  }
 







}
  







